import dbase using "$basein/Planea\PB_2018\directores_ctx_18.dbf", clear



gen Cursos=(R007=="1") if !missing(R007)
label var Cursos "Ever"
gen Curso12Meses=(R009=="1") if !missing(R009)
label var Curso12Meses "Past 12 months"



gen PlanEstudio=(R078=="3" | R078=="4") if !missing(R078)
label var PlanEstudio "Activities to improve learning"

gen ObservacionAula=(R083=="3" | R083=="4") if !missing(R083)
label var ObservacionAula "Classroom observations"

gen HabilidadesPedagogicas=(R085=="3" | R085=="4") if !missing(R085)
label var HabilidadesPedagogicas "Help teachers improve pedagogical practices"

gen InfoPadres=(R087=="3" | R087=="4") if !missing(R087)
label var InfoPadres "Provide parents with performance information"



rename CCT school_id

*********************************************************
***Traigo base con variable de tratamiento y controles***
*********************************************************

merge m:1 school_id using "$basein/Datos adicionales/base_Contraste.dta", gen(planea)

keep if planea==1 | planea==3


merge m:1 school_id using "$basein/Datos adicionales/Experimento_id.dta", gen(experimento)
drop experimento


merge m:1 school_id using "$basein/Datos adicionales/estratos.dta", gen(_estrato)

keep if _estrato==3


******************
***KEEP´ONLY PEC VS PEC PLUS******
******************

drop if state==10
drop if treatgroup2=="Control"
*************
**Id estado**

gen esi=substr(school_id, 1,2)

**********************
**Genero tratamiento**

gen PEC_Plus=0
replace PEC_Plus=1 if treatgroup2=="PEC Plus"

gen PEC=0
replace PEC=1 if treatgroup2=="PEC"

gen Control=0
replace Control=1 if treatgroup2=="Control"



***************************************************
***************************************************
*****Regresiones básicas ******
***************************************************
***************************************************
foreach var of varlist R078-R087{
	gen `var'_pca=(`var'=="3" | `var'=="4") if !missing(`var')

}
pca *_pca
predict index 
reghdfe index PEC_Plus if treatgroup!="Control" & state!=10,  vce(cluster school_id) absorb(i.estrato)
drop *_pca


********************
**PEC PLUS vs. PEC**


	eststo clear
	 eststo General:  my_ptest_pec_vs_pecplus Cursos Curso12Meses, by(treatgroup2) clus_id(school_id) strat_id(estrato)
	 esttab General  using "$tables/PlaneaContexto_Cursos.tex", label replace  nolines nogaps fragment ///
	 star(* 0.1 ** 0.05 *** 0.01)  ///
	 mlabels(none) nomtitles nonumbers noobs nodep collabels(none)  ///
     cells("mu_2(fmt(%9.2fc)) mu_3(fmt(%9.2fc) ) dif2(fmt(%9.2fc) star pvalue(d_p2))" "se_2(par) se_3(par) des_2(par)") 
	 
	 
	 eststo clear
	  eststo General:  my_ptest_pec_vs_pecplus PlanEstudio-InfoPadres, by(treatgroup2) clus_id(school_id) strat_id(estrato)
	 esttab General  using "$tables/PlaneaContexto_Actions.tex", label replace  nolines nogaps fragment ///
	 star(* 0.1 ** 0.05 *** 0.01)  ///
	 mlabels(none) nomtitles nonumbers noobs nodep collabels(none)  ///
     cells("mu_2(fmt(%9.2fc)) mu_3(fmt(%9.2fc) ) dif2(fmt(%9.2fc) star pvalue(d_p2))" "se_2(par) se_3(par) des_2(par)") 
	 
	 

	 	 
	 

foreach var of varlist Cursos-InfoPadres{
		
	 
	 
		reghdfe `var'  TD1 TD2 if treatgroup!="Control" & state!=10,  vce(cluster school_id) absorb(i.estrato)
		sum `var' if e(sample)==1
		local tempm=string(r(mean), "%9.2gc")
		file open newfile using "$tables/`var'_General_mean.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		
		sum `var' if TD1==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_General_Control.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		sum `var' if TD2==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_General_PecPlus.tex", write replace
		file write newfile "`tempm'"
		file close newfile

		sum `var' if (TD1==1 | TD2==1) & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_General_Dif2.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		
		
}


